import util from "../../util";
import parentInstance from "../parentInstance";
export default class template extends parentInstance {
    constructor() {
        super();
        this.util = util;
        this.typeList=['templateDictionary','templateTree','templateQueue','templateSingleton'];
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap=null;
        this.hier=null;
        this.others=null;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {
            fieldname；fieldtype
            },
          }
         */
    }
    static create(record){
        let object=new template();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    setRecord(record){
        this.record=record;
        this._setPointer();
        this._setHier();
        this._setOthers();
    }
    _setPointer(empty=true){
        if(!empty){
            this.info=null;
            this.pointer=null;
            this.modelMap=null;
            this.hier=null;
            this.others=null;
            return;
        }
        this.pointer={
            id:this.record.id,
            type:this.record.type,
            className:this.record.className,
        };
        this.info={
            id:this.record.id,
            name:this.record.name,
            type:this.record.type,
            className:this.record.className,
            version:this.record.version,
            published:this.record.published,
        };
        // this.modelMap=this._getModelMap();
    }
    _setHier(hier){
        if(!hier){
            this.hier = this.record.hier;
            return;
        }
        this.hier = this.record.hier = hier;
    }
    _setOthers(others){
        if(!others){
            this.others = this.record.others;
            return;
        }
        this.others = this.record.others = others;
    }
    _getModelMap(){
        return util.modelState.map.get([this.pointer.type,this.pointer.className].join(','));
    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(template.tpl(),record);
        let result = await util.api.TemplateInstance.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 删除
     * @returns {Promise<boolean|*>}
     */
    async delete(filter){
        let result = await util.api.TemplateInstance.delete({condition:filter}).then(flag=>{
            if(flag){
                this.setRecord({});
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;

    }
    async deleteById(id){
        let result = await util.api.TemplateInstance.deleteById(id).then(flag=>{
            if(flag){
                this.setRecord({});
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;

    }
    /**
     * 更新
     * @returns {Promise<*>}
     */
    async update(){
        let updateRec={
            id: this.record.id,
            type:this.record.type,
            className:this.record.className,
            name:this.record.name,
            version: this.record.version,
            published: this.record.published,
            features:this.record.features,
            values:this.record.values,
            hier:this.record.hier,
            web:this.record.web,
            others:this.record.others,
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: this.record.comments
        }
        let result = await util.api.TemplateInstance.update(updateRec).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 查询
     * @param param
     * @returns {Promise<*>}
     */
    async queryById(id){
        let result = await util.api.TemplateInstance.query({condition:`id=${id}`}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async query(filter){
        let result = await util.api.TemplateInstance.query({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    
	/**
	 * 运行时注册,更新hier中某项
	 * @param pointer 注册指针
	 * @param hier  转换成map,如:{"basic":[]}
	 * @returns {*}
	 */
	async runRegister(pointer,hier) {
        let result=await util.api.LogicInstance.runRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
	}

    async runAddRegister(pointer,hier){
        let result = await util.api.TemplateInstance.runAddRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    
    async runAllAddRegister(pointer,hier){
        let result = await util.api.TemplateInstance.runAllAddRegister(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async runRemoveRegister(pointer,names){
        let result = await util.api.TemplateInstance.runRemoveRegister(pointer,names).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    async runAddCustom(pointer,hier){
        let result = await util.api.TemplateInstance.runAddCustom(pointer,hier).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async runRemoveCustom(pointer,names){
        let result = await util.api.TemplateInstance.runRemoveCustom(pointer,names).then(data=>{
            if(!!data){
                this._setHier(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    
    /**
     * 通过其他模型中的others获取信息
     * @param {*} id
     * @returns
     */
    getOtherById (id) {
        if (!this.hier || this.hier.length === 0) {
            return {}
        }
        this._setPointer();
        let otherInfo = {};
        // 找到节点
        let index = this.hier[0].basic.findIndex(d => String(d.id) === String(id))
        if (index !== -1) {
            otherInfo = this.hier[0].basic[index].values
        }
        return otherInfo;
    }
    
    /**
     * hier的basic是否有值
     */
    isValidHier(){
        if (!this.hier ||this.hier.length===0 || !this.hier[0].basic || this.hier[0].basic.length === 0) {
            return false;
        }else{
            return true
        }
    }
    /**
     * others的basic是否有值
     */
    isValidOthers(){
        if (!this.others || !this.others.basic || this.others.basic.length === 0) {
            return false;
        }else{
            return true
        }
    }
    /**
     * 新增模板
     * @returns {{comments: string, web: {}, values: {}, name: string, className: string, published: boolean, type: string, version: number, hier: *[], others: {}}}
     */
    static tpl(){
        return {
            // id: "",
            name: "",
            type:"",
            className:"",
            version: "1.0",
            published: true,
            values:{},
            hier:[],
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }
}
